<?php
namespace Src\utils;

use InvalidArgumentException;
/**
 * 异常枚举类
 * 定义系统中所有可能的异常类型及对应的状态码
 */
class ResponseEnum
{
    // 系统级错误
    const SYSTEM_ERROR = ['code' => 500, 'message' => '系统内部错误'];
    const DB_ERROR = ['code' => 501, 'message' => '数据库操作失败'];
    const CACHE_ERROR = ['code' => 502, 'message' => '缓存操作失败'];
    const FILE_ERROR = ['code' => 503, 'message' => '文件操作失败'];

    // 客户端错误
    const PARAM_ERROR = ['code' => 400, 'message' => '参数错误'];
    const AUTH_ERROR = ['code' => 401, 'message' => '未授权访问'];
    const PERMISSION_DENIED = ['code' => 403, 'message' => 'Permission denied'];
    const RESOURCE_NOT_FOUND = ['code' => 404, 'message' => '资源不存在'];
    const METHOD_NOT_ALLOWED = ['code' => 405, 'message' => '请求方法不允许'];
    const REQUEST_TOO_FREQUENT = ['code' => 429, 'message' => '请求过于频繁'];

    // 业务逻辑错误
    const BUSINESS_ERROR = ['code' => 515, 'message' => '业务逻辑错误'];
    const DATA_EXIST = ['code' => 516, 'message' => '数据已存在'];
    const DATA_NOT_FOUND = ['code' => 517, 'message' => 'no data'];
    const OPERATE_FAILED = ['code' => 518, 'message' => '操作失败'];
    const VALIDATE_FAILED = ['code' => 519, 'message' => '数据验证失败'];

    /**
     * 获取异常信息
     * @param string $exceptionType 异常类型常量名
     * @return array 包含code和message的数组
     * @throws InvalidArgumentException 如果异常类型不存在
     */
    public function getExceptionInfo($exceptionType)
    {
        if (!defined("self::{$exceptionType}")) {
            throw new InvalidArgumentException("未知的异常类型: {$exceptionType}");
        }

        return self::${$exceptionType};
    }

    /**
     * 获取异常状态码
     * @param string $exceptionType 异常类型常量名
     * @return int 状态码
     */
    public function getCode($exceptionType)
    {
        $info = self::getExceptionInfo($exceptionType);
        return $info['code'];
    }

    /**
     * 获取异常消息
     * @param string $exceptionType 异常类型常量名
     * @return string 异常消息
     */
    public function getMessage($exceptionType)
    {
        $info = self::getExceptionInfo($exceptionType);
        return $info['message'];
    }
}
?>